package day11;

/**
 * 面试的死锁程序
 * 当同步嵌套然后锁不一样，容易出现死锁
 * @author wangbiao
 *
 */
public class DeathLock implements Runnable{
	private boolean flag;

	public DeathLock(boolean flag) {
		this.flag = flag;
	}
	
	@Override
	public void run() {
		if (flag) {
			synchronized(this) {
				System.out.println("if lock this");
				synchronized(DeathLock.class) {
					System.out.println("if lock DeathLock.class");
				}
			}
		} else {
			synchronized(DeathLock.class) {
				System.out.println("else lock DeathLock.class");
				synchronized(this) {
					System.out.println("else lock this");
				}
			}
		}
		
	}

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Thread(new DeathLock(true)).start();
		new Thread(new DeathLock(false)).start();
	}
	
	

}

